// pages/my/history/list.js

const app = getApp();
const util = require("../../../utils/util");
const requestUtil = require("../../../utils/request");
const api = require("../../../config/api");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: app.globalData.CustomBar,
    hasNoMoreHistory: false, // 没有更多记录
    hasHistoryList: false, // 判断是否有历史记录
    userType: 1,
    cardInfo: null,
    pageNo: 0, // 页码
    pageNum: 0, // 每页展示数
    historyList: [],

  },

  onShow: function () {
    let userType = app.globalData.userType;
    let cardInfo = userType == 1 ? app.globalData.employeeInfo : userType == 2 ? app.globalData.customInfo : null;
    this.setData({
      userType,
      cardInfo,
      pageNo: app.globalData.pageNo,
      pageNum: app.globalData.pageNum
    })
    this.getHistoryList();
  },
  lower(e) {
    if (!this.data.hasNoMoreHistory) {
        this.setData({
          pageNo: this.data.pageNo + 1,
        });
        this.getHistoryList();
    }
  },
  /**
   * 获取历史记录列表
   */
  getHistoryList: async function () {
    let params = {
      userType: this.data.userType,
      pageIndex: this.data.pageNo,
      pageNum: this.data.pageNum
    };
    if (this.data.userType == 1) {
      params.userId = this.data.cardInfo.id;
    } else {
      params.custId = this.data.cardInfo.id;
    }
    let res = await requestUtil.request("POST", api.urls.getHistoryList, params);
    if (res.data.code != 0) {
      util.showToast(res.data.msg);
      return;
    }
    let historyList = res.data.data;
    // 格式化日期
    for (let i = 0; i < historyList.length; i++) {
      let arr = util.formatChineseDate(new Date(historyList[i].createTime.replace(/-/g, '/'))).split('-');
      let time = {
        year: arr[0],
        month: arr[1],
        day: arr[2]
      }
      historyList[i].visitedDate = time;
      if (historyList[i].employeeHeadImgUrl != null) {
        let url = historyList[i].employeeHeadImgUrl;
        historyList[i].employeeHeadImgUrl = api.picUrl + url;
      }
    }
    let list = this.data.historyList;
    list.push(...historyList);
    let hasNoMore = true;
    if (historyList.length <= this.data.pageNum) {
      hasNoMore = false
    }
    this.setData({
      historyList: list,
      hasHistoryList: true,
      hasNoMoreHistory: hasNoMore
    })
  },

  /**
   * 点击查看更多
   */
  moreDetail: function (e) {
    let id = e.currentTarget.dataset.id;
    // 固定，历史记录页面，顾客可查看员工信息
    wx.navigateTo({
      url: '../../card/card?id=' + id + '&type=' + 1,
    })
  }
})